<!DOCTYPE html>
<% def dateFormat = new java.text.SimpleDateFormat("yyyy-M-d H:m:s") %>
<html>
    <head>
        <style type='text/css'>

            body {
                font-family: arial;
                margin: 0;
                padding: 0;
                color: #333;
            }
            #hd {
                padding: 0 1em 1em 1em;
                background-color: #f0f0f0;
            }
            
            h1 {
            	padding-top: 0.5em;
            	margin-top: 0;
            }
            
            #bd {
                padding: 0.5em 1em 1em 1em;
            }
            .docLabel, .constraintLabel { font-weight: bold; margin-right: 1em; }
            .constraintLabel { xcolor: orange; }
            .creationDate  { font-size: 80%; font-style: italic; color: #555; }
            .creationDate a:link, .creationDate a:visited { color: inherit; text-decoration: none; } 
            .creationDate a:hover, .creationDate a:active { color: inherit; text-decoration: underline; } 
            
            .timeline {
            	height: 2em;
            	font-size: 70%;
            	margin-left: 30px;
            	position: relative;
            }
            .timerow {
            	position: relative;
            	color: white;
            }
            .timeblock {
            	position: absolute;
            	padding: 0.2em 0;
            	text-align: center;
				-webkit-border-radius: 3px;
				-moz-border-radius: 3px;
				border-radius: 3px;            	
				background-color: #41568D ;
				background-color: #90B96F;
			}
			
			.timeblock.right {
				right: 0px;
			}
            
            .timeblock.failed {
           		background-color: #E64E4E;
            }
            
            .statusMark.fail {
           		color: #ff5757;
           		font-weight: bold;
            }
            
           .timeblock.duration {
	           color: black;
           	   background-color: white;
            } 
            
            <%
              // this is not a real border, but actually the line that links the pipeline
              // stage through the middle
            %>
            .timelineborder {
	            border-top: solid 1px #41568D;
				height: 1px;
				position: absolute;
				top: 45%;
				width: 90%; 
            }
            
            <%
               // this is a hack: it's hard to get the border all the way to the right
               // without overflow, causing scrollbars (because we are using percentages)
               // so actually we draw two borders: one right aligned and one left aligned
            %>
            .timelineborder.right {
            	right: 2px;
            }
            
            a.stageLink:link, a.stageLink:visited {
            	color: white;
            	text-decoration: none;
            }
            a.stageLink:hover, a.stageLink:active {
            	color: white;
            	text-decoration: underline;
            }
            
            hr {
            	margin-top: 2.8em;
            }
            
			h2.hr {
			    font-size: 16pt;
			    color: #444;	
				position: relative;
				top: -1em;
				margin-top: 0;
				background-color: white;
				float: left;
				margin-left: 20px;
				padding: 0px 10px;
			}
			
			.stageList {
			     background-color: #F0F0F0;
			     list-style-type: none;
			     -webkit-border-top-left-radius: 2px;
			     -webkit-border-top-right-radius: 2px;
			     -moz-border-radius-topleft: 2px;
			     -moz-border-radius-topright: 2px;
			     border-top-left-radius: 2px;
			     border-top-right-radius: 2px;
			}
			 
			.stageList table {
				font-size: 10pt;
			}
			.stageList th {
			 	text-align: right;
			 	padding-right: 0.5em;
			 }
			 .stageList td {
			 	text-align: left;
			 	padding-right: 0.5em;
			 	background: #FCFCFC;
				padding: 3px 0.5em;
			 }
				 
			 .stageListOuter {
			 	padding-left: 30px;
			 	clear: both;
			 }
			 .stageList h3 {
			   box-shadow: 1px 1px 3px black;
			   position: relative;
			   padding: 0.5em;
			   background-color: #41568D;
			   color: white;
			     -webkit-border-top-left-radius: 3px;
			     -webkit-border-top-right-radius: 3px;
			     -moz-border-radius-topleft: 3px;
			     -moz-border-radius-topright: 3px;
			     border-top-left-radius: 3px;
			     border-top-right-radius: 3px;
			     font-size: 12pt;
			 }
			 a:target {
			   color: yellow !important;
			 }
			 
			 .stageList .bd {
			   margin: 0.5em;            
			   padding-bottom: 1em;
		     }
		     
		     .statusMark {
		     	position: absolute;
		     	right: 1em;
		     }
		     div.status {
		     	clear: both;
		     	margin-left: 30px;
		     	font-size: 110%;
		     }
		     div.status.success {
		     	color: green;
		     }
		     div.status.fail {
		     	color: #e01f1f;
		     	font-weight: bold;
		     }
		     p.descBody { 
		     	font-size: 92%; 
	     	 }
	     	 .branchTitle {
	     	 	clear: both;
	     	 	text-decoration: underline;
	     	 	font-size: 95%;
	     	 }
        </style>
    </head>
    <body>
        <div id='hd'>
            <h1>${pipeline.documentation.title}</h1>
            <p class='creationDate'>Created by <a class='unobtrusiveLink' href='http://bpipe.org'>Bpipe</a>, ${new Date()}</p>
        </div>

        <div id='bd'>
        	<hr>
            <h2 class='hr'>Result</h2>
            <br class='h2hr'>
            <div class='status <%=pipeline.failed?"fail":"success"%>'>
	            <%if(pipeline.failed) {%>
	            Failed in stage <%=stages.flatten().reverse().find { list -> list.find {!it.succeeded} }?.context?.stageName%>
	            <%} else {%>
	            Succeeded
	            <%}%>
            </div>
            
        	<hr>
            <h2 class='hr'>Timeline</h2>
            <br class='h2hr'>
        	<%stages.each { branch -> %>
            <div class='timeline'>
            	<div class='timelineborder'> </div>
            	<div class='timelineborder right'> </div>
		            	<%long totalTimeMs = branch*.context*.documentation.grep{it != null}.sum{ it.elapsedMs?:0 }; int count=0; int branchCount=0;%>
		            	<%def widths=[] %>
			            <%branch.each { stage ->  def ctx = stage.context; 
			            	     	def width = (ctx.documentation.elapsedMs?:0)/(float)totalTimeMs*100;
			            	     	if(width<5.0)
			            	     		width=5.0f
			            	     	widths << width;
	        	     	 }
				         // width can be greater than 100%, so scale here:
				         def total = widths.sum()
				         widths = widths.collect { it / (total/100.0f)  }
				         // Make sure the last width is right aligned
				         if(widths)
				            widths[-1] += 100.0f - widths.sum() + 1.0f
	        	     	%>
			            
			            <%
			            float left = 0; 
			            branch.each { stage ->  def ctx = stage.context; branchCount++; %>
			            <div class='timerow'>
			            	<div <% 
			            	     def width = widths[count++];
		            	     	 %>
		            	     	 class='timeblock <%if(!stage.succeeded){%>failed<%}%> <%=(branch && stage==branch[-1])?"right":""%>' 
		            	     	 style='left: ${left}%;  width: ${width-1}%'>
		            	     	 <%
		            	     	   // Just a heuristic here, we roughly assume 1% width per char (for 1024px, 10px char width)
		            	     	   // So truncate when text is wider than that. The -1 nudges it down for the extreme case
		            	     	   // so that the minimum 5% only gets 4 characters before truncation
		            	     	 %>
		            	     	 <a class='stageLink' href='#stage_${branchCount}_${ctx.stageName}'><%=bpipe.Utils.truncnl(ctx.stageName,(int)(width-1))%></a>
		            	     	 </div>
		            	     	 <% left += width %>
				            </div>
			            <%}%>
	            </div>
	            <div class='timeline'>
		            	<%count = 0 %>
		            	<%left = 0 %>
			            <%branch*.context.each { stage -> %>
			            <div class='timerow'>
			            	<div <% 
			            	     	def width = widths[count++]
		            	     	 %>
		            	     	 class='timeblock duration' style='left: ${left}%;  width: ${width-1}%'>
		            	     	 
		            	     	 <%if(stage.documentation?.finishedAt) {%>
	                              <% use(groovy.time.TimeCategory) { %>
	                                <%=(stage.documentation.finishedAt - stage.documentation.startedAt).toString().replaceAll('seconds','s').replaceAll('minutes','mins')%>
	                                <% } %> 
	                            <% } %> 
		            	     	 </div>
		            	     	 
		            	     	 <% left += width %>
			            </div>
			            <%}%>
	            </div>
            <%}%>
            
            
        	<hr>
            <h2 class='hr'>Pipeline Stages</h2>
            <br class='h2hr'>
            <% int branchCount = 0; %>
        	<%stages.each { branch -> ++branchCount; %>
            <ol class='stageListOuter'>
            
            	<%if(stages.size()>1) {%>
            		<p class='branchTitle'>Branch ${branchCount}</p>
        		<%}%>
            
	            <%branch.each { stage -> def ctx = stage.context; %>
	                <li class='stageList'>
	                    <h3 class='stageTitle'><a name='stage_${branchCount}_${ctx.stageName}'>
	                        <% if(ctx.documentation?.title) { %>
	                            ${ctx.stageName} : ${ctx.documentation.title} 
	                        <%} else {%>
	                            ${ctx.stageName}
	                        <% } %>
	                        </a>
	                        
	                        <span class='statusMark <%=stage.succeeded?"success":"fail"%>' >
	                        <%if(stage.succeeded) {%>
	                        &#10003;
	                        <%} else {%>
	                        X
	                        <%}%>
	                        </span>
	                    </h3>
	                    <div class='bd'>
	
	                    <%if(ctx.documentation) {%>
	                        <% def doc = ctx.documentation; %>
	                        <%if(doc.desc) {%>
	                            <p class='descBody'>${doc.desc}</p>
	                        <%}%>
	
							<table>
	                        <%if(doc.author) {%>
							<tr>
	                            <th class='authorBody'>Author</th>
	                            <td>${doc.author}</td>
	                        </tr>
	                        <%}%>
	
	                        <%if(doc.inputs || ctx.inputs) {%>
							<tr>
	                            <th class='docLabel'>Inputs</th>
	                        	<%if(ctx.inputs) {%>
		                            <td>
		                            <ul class='inputs'>
	                                <% ctx.inputs.each { inp -> %>
	                                    <li class='input'>${inp.replaceAll("^\\./","")}</li>
	                                <% } %>
	                                </ul>
	                                </td>
	                        	<%} else {%>
		                            <% if(doc.inputs instanceof List) { %>
		                                <td>
		                                <ul class='inputs'>
			                                    <% doc.inputs.each { inp -> %>
			                                        <li class='input'>${inp}</li>
			                                    <% } %>
		                                </ul>
		                            <% } else { %>
	                                    <td>${doc.inputs}</td>
		                            <% } %> 
	                            <% } %>
	                            </td>
	                        </tr>
	                        <%}%>
                            
                            <tr><th>Commands</th><td>${ctx.trackedOutputs.keySet().join("<br>")}</td></tr>
	
	
	                        <%if(doc.outputs || ctx.outputs) {%>
							<tr>
	                            <th class='docLabel'>Outputs</th>
	                            <% if(ctx.outputs) { %>
	                            <td>
	                            	<ul class='outputs'>
	                            	<%ctx.outputs.each { output ->%>
	                            		<li>${output.replaceAll("^\\./","")}</li>
	                            	<%}%>
	                            	</ul>
	                        	</td>
	                            <% } else { %>
		                            <% if(doc.outputs instanceof List) { %>
		                            <td>
		                                <ul class='outputs'>
		                                    <% doc.outputs.each { inp -> %>
		                                        <li class='input'>${inp}</li>
		                                    <% } %>
		                                </ul>
		                            </td>
		                            <% } else { %>
		                                <td>${doc.outputs}</td>
		                            <% } %>
	                            <% } %>
	                        </tr>
	                        <%}%>
	
	                        <%if(doc.tools) {%>
							<tr>
	                            <th class='docLabel'>Tools</th>
	                            <td>
	                            <ul class='tools'>
	                                <% doc.tools.each { name, detail -> %>
	                                  <li class='input'>
	                                  	<% if(detail.meta.link) { %>
	                                  		<a href='${detail.meta.link}'>${name}</a>
	                                  	<% } else { %>
	                                  		${name}  
		                                <% } %> :
	                                    ${detail.version}
	                                    
	                                  	<% if(detail.meta.desc) { %>
	                                  	 - <span class=toolDesc>${detail.meta.desc}</span>
		                                <% } %> 
	                                  </li>
	                                <% } %>
	                            </ul>
	                            </td>
	                        </tr>
	                        <%}%>
	
	                        <%if(doc?.constraints) {%>
							<tr>
	                            <th class='constraintLabel'>Constraints</th>
	                            <td>${doc.constraints}</td>
	                        </tr>
	                        <%}%>
	                        
	                        <%if(doc.startedAt) {%>
							<tr>
	                            <th class='docLabel'>Execution Time</th>
	                            <td>
	                            	<%if(doc.elapsedMs<1000 || !doc.finishedAt) { %>
		                                ${dateFormat.format(doc.startedAt)} 
	                                <% } else {%>
		                                ${dateFormat.format(doc.startedAt)} - ${dateFormat.format(doc.finishedAt)}
	                                <% } %>
	                                <%if(doc.finishedAt) use(groovy.time.TimeCategory) { %>
	                                (${doc.finishedAt - doc.startedAt})
	                                <% } %>
	                            </td>
	                        </tr>
	                        <%}%>
							</table>
	                    <%}%>
	                    
	                    </div>
	                </li>
	            <%}%>
	            </ol>
	        <%}%>
	        </div>
    </body>
</html>
